<?php
/**
 * Nuwani PHP IRC Bot Framework
 * Copyright (c) 2006-2010 The Nuwani Project
 *
 * Nuwani is a framework for IRC Bots built using PHP. Nuwani speeds up bot
 * development by handling basic tasks as connection- and bot management, timers
 * and module managing. Features for your bot can easily be added by creating
 * your own modules, which will receive callbacks from the framework.
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 * @copyright Copyright (c) 2006-2010 The Nuwani Project
 * @package Nuwani
 * @author Peter Beverloo <peter@lvp-media.com>
 * @see http://nuwani.googlecode.com
 */

require_once __DIR__ . '/../src/Sources/SecurityManager.php';

use Nuwani \ SecurityManager;
use Nuwani \ User;

class SecurityManagerTests extends PHPUnit_Framework_TestCase
{
        /**
         * @author Dik Grapendaal <dik.grapendaal@gmail.com>
         * @covers SecurityManager::initialize
         * @since 2011-08-21
         * @test
         */
        public function configurationInitialization ()
        {
                $users = array
                (
                        array
                        (
                                'Username'      => '*!username@hostname.com',
                                'Password'      => false,
                                'Permissions' 	=> array
                                (
										'owner'
                                )
                        )
                );
                $securityManager = new SecurityManager ();
                $securityManager -> initialize ($users);
                
                $user = new User ('', 'nickname!username@hostname.com');
                $this -> assertTrue ($securityManager -> isBotOwner ($user), 'User from configuration is not granted the owner permission.');
        }
        
        /**
         * @author Dik Grapendaal <dik.grapendaal@gmail.com>
         * @covers SecurityManager::grantPermission
         * @covers SecurityManager::revokePermission
         * @since 2011-08-21
         * @test
         */
        public function grantAndRevokePermission ()
        {
                $securityManager = new SecurityManager ();
                $user = $securityManager -> addUser ('nickname!ident@hostname.nl', false);
                $this -> assertFalse ($securityManager -> hasPermission ($user, 'owner'), 'Initial permissions should be empty, owner permission found.');
                $this -> assertFalse ($securityManager -> hasPermission ($user, 'test'), 'Initial permissions should be empty, test permission found.');

                $securityManager -> grantPermission ($user, 'test');
                $this -> assertFalse ($securityManager -> hasPermission ($user, 'owner'), 'Owner permission is never set, should not be there.');
                $this -> assertTrue ($securityManager -> hasPermission ($user, 'test'), 'User does not have the test permission after being set.');
                
                $securityManager -> revokePermission ($user, 'test');
                $this -> assertFalse ($securityManager -> hasPermission ($user, 'owner'), 'Test permission was removed, owner permission was added.');
                $this -> assertFalse ($securityManager -> hasPermission ($user, 'test'), 'Test permission was not revoked.');
        }
        
        /**
         * @author Dik Grapendaal <dik.grapendaal@gmail.com>
         * @covers SecurityManager::identifyUser
         * @covers SecurityManager::logoutUser
         * @since 2011-08-21
         * @test
         */
        public function identifyAndLogoutUser ()
        {
                $users = array
                (
                        array
                        (
                                'Username'      => '*!username@hostname.com',
                                'Password'      => '098f6bcd4621d373cade4e832627b4f6',
                                'Permissions' 	=> array
                                (
                                        'owner'
                                )
                        )
                );
                $securityManager = new SecurityManager ();
                $securityManager -> initialize ($users);
                
                $user = new User ('', 'nickname!username@hostname.com');
                $this -> assertFalse ($securityManager -> hasPermission ($user, 'owner'), 'User has owner permission before logging in.');
                
                $securityManager -> identifyUser ($user, md5('test'));
                $this -> assertTrue ($securityManager -> hasPermission($user, 'owner'), 'User does not have owner permission after logging in.');
                
                $securityManager -> logoutUser ($user);
                $this -> assertFalse ($securityManager -> hasPermission ($user, 'owner'), 'User still has owner permission after logging out.');
        }
}